package personal.web.mvc;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class ControllerExceptionHandler {
	private static Logger log = Logger.getLogger("accounting");

	/**
	 * Logs exception in application log and returns message to client
	 * @param e Runtimeexception
	 * @param response HTTP Servlet Response
	 */
	public void handleRuntimeException(RuntimeException e, HttpServletResponse response) {
		try {
			response.getWriter().write("{\"error\": \"Unexpected error: " + e.getMessage() + "\"}");
		} catch (IOException e1) {
			log.error("Error while writing runtime exception to response", e);
		}
		log.error("Uncaught exception", e);
	}
}
